/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.gs.meta.ops;

import cn.gs.meta.db.tables.Keep;
import cn.gs.meta.db.tables.Obj;
import cn.gs.meta.db.tables.Ogn;
import cn.gs.util.Eval;
import cn.gs.util.HibernateUtil;
import junit.framework.Assert;
import junit.framework.TestCase;
import org.junit.AfterClass;
import org.junit.BeforeClass;

/**
 *
 * @author gs
 */
public class OgnOpsTestBase extends TestCase {

    protected Obj obj, animal, factory, bullprod, milkprod;
    protected Ogn ogn;
    protected Obj food, fruit, orange, watermelon, meat, beef, pork, bull, milk, cow, grass, oil;
    protected OgnOps ops;

    @BeforeClass
    @Override
    public void setUp() throws Exception {
        HibernateUtil.getSession().beginTransaction();
        ogn = getOgn("ogn");
        ops = new OgnOps();
        ops.getOps().setOgn(ogn);
        obj = getObj("obj");
        animal = getObj("animal");
        factory = getObj("factory");
        food = getObj("food");
        fruit = getObj("fruit");
        orange = getObj("orange");
        watermelon = getObj("watermelon");
        meat = getObj("meat");
        beef = getObj("beef");
        pork = getObj("pork");
        bull = getObj("bull");
        milk = getObj("milk");
        cow = getObj("cow");
        grass = getObj("grass");
        bullprod = getObj("bullprod");
        milkprod = getObj("milkprod");
        oil = getObj("oil");
    }

    @AfterClass
    @Override
    public void tearDown() throws Exception {
        HibernateUtil.getSession().getTransaction().rollback();
    }

    protected void addExist() {
        bullprod.getBuilt().put(grass, -60.0);
        bullprod.getBuilt().put(oil, -10.0);
        bullprod.getDel().put(beef, 20.0);
    }

    protected void addChg() {
        bullprod.getDailyChg().put(bull, -2.0);
        bullprod.getDailyChg().put(beef, 10.0);
        cow.getDailyChg().put(cow, 0.1);
        orange.getDailyChg().put(watermelon, -.1);
        watermelon.getDailyChg().put(orange, 0.2);
    }

    protected void addUsed() {
        factory.getUsedChg().put(oil, -4.0);
        cow.getUsedChg().put(grass, -2.5);
        bullprod.getUsed().put(factory, -2.0);
        bullprod.getUsed().put(cow, -3.0);
        milkprod.getUsed().put(cow, -1.5);
    }

    protected void addReplace() {
        food.getRep().put(fruit, 1.5);
        fruit.getRep().put(orange, 1.2);
        fruit.getRep().put(watermelon, 1.5);
        food.getRep().put(meat, 3.0);
        meat.getRep().put(beef, 1.2);
        meat.getRep().put(pork, 2.7);
        food.getRep().put(milk, 2.0);
        animal.getRep().put(cow, 4.0);
    }

    protected static Obj getObj(String name) {
        Obj m = new Obj();
        m.setName(name);
        m.getRep().clear();
        m.getBuilt().clear();
        m.getDel().clear();
        m.getDailyChg().clear();
        m.getUsed().clear();
        m.getUsedChg().clear();
        ModelBaseOps.save(m);
        return (Obj) m;
    }

    protected static Ogn getOgn(String name) {
        Ogn m = new Ogn();
        m.setName(name);
        ((Ogn) m).getKeeps().clear();
        ModelBaseOps.save(m);
        return (Ogn) m;
    }
    
    public static void assertArrayEqual(double[] a, double[] b) {
        Assert.assertTrue(Eval.arrayToStr(a) + " != " + Eval.arrayToStr(b), Eval.equal(a, b));
    }
}